﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlPhieu : System.Web.UI.UserControl
{
    MethodClassCommon common;
    BCDataCenter.DataAccessLayer.DAL dal;
    SqlConnection con;
    SqlCommand command,get_priceRate;
    private string str = BCDataCenter.Configuration.CurrentConnectionString;
    SqlTransaction tran;
    SqlCommand Insert_RTHDR, Insert_RTLIN,  UPDATE_FILTER_RTARCUST, GET_INVNO, UPDATE_NextRTInvNo;
    string error = "";
    SqlCommand UPDATE_RTHDR, DELETE_RTLIN;
    static bool flag = true;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["phieumuahang"] = new System.Collections.Generic.List<ClassPhieuMuaHang>();
            ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Clear();
            txtNgay.Text = System.DateTime.Now.Date.ToString("dd/MM/yyyy");
            BindDropHangNhap();
        }
        
    }
    string GetInvo1()
    {
        con = new SqlConnection(str);
        con.Open();
        GET_INVNO = new SqlCommand();
        GET_INVNO.Connection = con;
        GET_INVNO.CommandText = "GET_INVNO";
        string invno = GET_INVNO.ExecuteScalar().ToString();
        con.Close();
        return invno;
    }
    int check(string proid)
    {
        int n = -1;
        for (int i = 0; i < ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Count; i++)
        {
            if (proid == ((List<ClassPhieuMuaHang>)Session["phieumuahang"])[i].MaHang1.ToString())
            {
                n = i;
            }

        }
        return n;
    }
    protected void BtNhapToSession_Click(object sender, ImageClickEventArgs e)
    {
        con=new SqlConnection(str);
        con.Open();
        get_priceRate=new SqlCommand("GET_RTPriceRate",con);
        get_priceRate.CommandType=CommandType.Text;
        float rate=float.Parse(get_priceRate.ExecuteScalar().ToString());
        command=new SqlCommand("GET_PRICE",con);
        command.CommandType=CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@itemkey",DropTenhangNhap.SelectedValue.ToString()));
        command.Parameters.Add(new SqlParameter("@RTPriceRate",rate));
        double price = System.Math.Round(float.Parse(command.ExecuteScalar().ToString()));
        con.Close();
        ClassPhieuMuaHang dt = new ClassPhieuMuaHang();
        dt.MaHang1 = DropTenhangNhap.SelectedValue.ToString();
        dt.TenHang1 = DropTenhangNhap.SelectedItem.ToString();
        if ((txtSoLuong.Text=="")||(txtSoLuong.Text==null))
        {
            dt.SoLuong1=1;
        }
        else
        {
           dt.SoLuong1 = float.Parse(txtSoLuong.Text.Trim());
        }
        dt.Gia1 = float.Parse(price.ToString());
        dt.LineType1 = false;
        for (int y = 0; y < GridView1.Rows.Count; y++)
        {
           
            CheckBox chon;
            chon = (CheckBox)GridView1.Rows[y].FindControl("CheckKM");
            int kt=check(GridView1.Rows[y].Cells[1].Text);
            if (kt < 0)
            {
                if (chon.Checked)
                {
                    dt.LineType1 = true;
                }
                else
                {
                    dt.LineType1 = false;
                }
            }
            else
            {
                if (chon.Checked)
                {
                    (((List<ClassPhieuMuaHang>)Session["phieumuahang"])[y]).LineType1 = true;
                }
                else
                {
                    (((List<ClassPhieuMuaHang>)Session["phieumuahang"])[y]).LineType1 = false;
                }
            }
        }
        int n = check(dt.MaHang1.ToString());
        if (n < 0)
        {
            
            ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Add(dt);
        }
        else
        {
            (((List<ClassPhieuMuaHang>)Session["phieumuahang"])[n]).SoLuong1 += dt.SoLuong1;
        }
        lblTongTien.Text =string.Format("{0:#,#}",tongtien());
        GridView1.DataSource = Session["phieumuahang"];
        GridView1.DataBind();
    }
    private float tongtien()
    {
        float tong = 0;
        for (int i = 0; i < ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Count; i++)
        {
            tong += (((List<ClassPhieuMuaHang>)Session["phieumuahang"])[i]).ThanhTien1;
        }
        return tong;
    }
    void BindDropHangNhap()
    {
        ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
        DataTable db = obj.Get_Data_DataTable(str, ref error, "k_getInforInmast", CommandType.StoredProcedure);
        DropTenhangNhap.DataSource = db;
        DropTenhangNhap.DataValueField = "itemkey";
        DropTenhangNhap.DataTextField = "description";
        DropTenhangNhap.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = Session["phieumuahang"];
        GridView1.DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = Session["phieumuahang"];
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        float soluong = float.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
        CheckBox chon;
        chon = (CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckKM");
        if (chon.Checked)
        {
            (((List<ClassPhieuMuaHang>)Session["phieumuahang"])[e.RowIndex]).LineType1=true;
        }
        else
        {
            (((List<ClassPhieuMuaHang>)Session["phieumuahang"])[e.RowIndex]).LineType1 = false;
        }
        (((List<ClassPhieuMuaHang>)Session["phieumuahang"])[e.RowIndex]).SoLuong1 = soluong;
        GridView1.EditIndex = -1;
        GridView1.DataSource = Session["phieumuahang"];
        GridView1.DataBind();
        lblTongTien.Text = Convert.ToString(tongtien());
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();
        for (int i = 0; i < ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Count; i++)
        {
            if (keys == ((List<ClassPhieuMuaHang>)Session["phieumuahang"])[i].MaHang1.ToString())
            {
                ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).RemoveAt(i);
            }
        }
        GridView1.EditIndex = -1;
        GridView1.DataSource = Session["phieumuahang"];
        GridView1.DataBind();
        lblTongTien.Text = Convert.ToString(tongtien());
    }
    void clear()
    {
        ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Clear();
        txtNgay.Text = System.DateTime.Now.Date.ToString("dd/MM/yyyy");
        txtMaSoKhachHang.Text = "";
        txtMaTuVanVien.Text = "";
        txtDiaChi.Text = "";
        txtTenKhachHang.Text = "";
        txtTuVanVien.Text = "";
        txtSoLuong.Text = "";
        lblTongTien.Text = "";
        GridView1.DataSource = null;
        GridView1.DataBind();
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        clear();
        TabContainer1.ActiveTabIndex = 0;
        lblKetQua.Visible = false;
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        flag = true;
    }
    protected void ImageTimKhachHang_Click(object sender, ImageClickEventArgs e)
    {
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        lblKetQua.Visible = false;
        TabContainer1.ActiveTabIndex = 1;
        MultiView1.ActiveViewIndex = 0;
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        lblKetQua.Visible = true;
        string invno;
        con = new SqlConnection(str);
        con.Open();
        tran = con.BeginTransaction();
        Insert_RTLIN = new SqlCommand();
        Insert_RTLIN.Connection = con;
        UPDATE_FILTER_RTARCUST = new SqlCommand();
        UPDATE_FILTER_RTARCUST.Connection = con;
        if (flag == true)
        {
            UPDATE_NextRTInvNo = new SqlCommand();
            UPDATE_NextRTInvNo.Connection = con;
            GET_INVNO = new SqlCommand();
            GET_INVNO.Connection = con;
            GET_INVNO.Transaction = tran;
            GET_INVNO.CommandText = "GET_INVNO";
            GET_INVNO.CommandType = CommandType.StoredProcedure;
            invno = GET_INVNO.ExecuteScalar().ToString();
            Insert_RTHDR = new SqlCommand();
            Insert_RTHDR.Connection = con;
            try
            {
                Insert_RTHDR.Transaction = tran;
                Insert_RTHDR.CommandText = "INSERT_RTHDR";
                Insert_RTHDR.CommandType = CommandType.StoredProcedure;
                Insert_RTHDR.Parameters.Add(new SqlParameter("@InvNo", invno));
                Insert_RTHDR.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text.Trim()));
                Insert_RTHDR.Parameters.Add(new SqlParameter("@SiteCode", "khoi"));
                Insert_RTHDR.Parameters.Add(new SqlParameter("@CustomerKey", txtMaSoKhachHang.Text.Trim()));
                Insert_RTHDR.Parameters.Add(new SqlParameter("@TotalAmount", float.Parse(lblTongTien.Text)));
                Insert_RTHDR.Parameters.Add(new SqlParameter("@InvDate", txtNgay.Text.Trim()));
                Insert_RTHDR.ExecuteNonQuery();
                int i = 0;
                foreach (GridViewRow grv in GridView1.Rows)
                {
                    Insert_RTLIN.Parameters.Clear();
                    Insert_RTLIN.Transaction = tran;
                    Insert_RTLIN.CommandText = "INSERT_RTLIN";
                    Insert_RTLIN.CommandType = CommandType.StoredProcedure;
                    CheckBox chon;
                    Insert_RTLIN.Parameters.Add(new SqlParameter("@InvNo", invno));
                    chon = (CheckBox)GridView1.Rows[i].FindControl("CheckKM");
                    if (chon.Checked)
                    {
                        Insert_RTLIN.Parameters.Add(new SqlParameter("@LineType", "True"));
                    }
                    else
                    {
                        Insert_RTLIN.Parameters.Add(new SqlParameter("@LineType", "False"));
                    }
                    Insert_RTLIN.Parameters.Add(new SqlParameter("@itemkey", grv.Cells[1].Text));
                    Insert_RTLIN.Parameters.Add(new SqlParameter("@Quantity", float.Parse(grv.Cells[3].Text)));
                    Insert_RTLIN.Parameters.Add(new SqlParameter("@Price", float.Parse(grv.Cells[5].Text)));
                    Insert_RTLIN.ExecuteNonQuery();
                    i++;
                }
                UPDATE_FILTER_RTARCUST.Transaction = tran;
                UPDATE_FILTER_RTARCUST.CommandText = "UPDATE_FILTER_RTARCUST";
                UPDATE_FILTER_RTARCUST.CommandType = CommandType.StoredProcedure;
                UPDATE_FILTER_RTARCUST.Parameters.Add(new SqlParameter("@CustomerKey", txtMaSoKhachHang.Text.Trim()));
                UPDATE_FILTER_RTARCUST.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
                UPDATE_FILTER_RTARCUST.Parameters.Add(new SqlParameter("@LastBuy", txtNgay.Text));
                UPDATE_FILTER_RTARCUST.Parameters.Add(new SqlParameter("@UTDOffTake", float.Parse(lblTongTien.Text)));
                UPDATE_FILTER_RTARCUST.ExecuteNonQuery();
                UPDATE_NextRTInvNo.Transaction = tran;
                UPDATE_NextRTInvNo.CommandText = "UPDATE_NextRTInvNo";
                UPDATE_NextRTInvNo.CommandType = CommandType.StoredProcedure;
                UPDATE_NextRTInvNo.Parameters.Add(new SqlParameter("@INVNO", Convert.ToString(float.Parse(invno) + 1)));
                UPDATE_NextRTInvNo.ExecuteNonQuery();
                tran.Commit();
                System.Windows.Forms.MessageBox.Show("Thêm phiếu hàng " + invno + " đã hoàn thành");
            }
            catch (Exception ex)
            {
                lblKetQua.Text = ex.Message;
                tran.Rollback();
            }

            finally
            {
                con.Close();
            }
        }
        else
        {
            invno = lblSoChungTu.Text;  
            UPDATE_RTHDR = new SqlCommand();
            UPDATE_RTHDR.Connection = con;
            DELETE_RTLIN = new SqlCommand();
            DELETE_RTLIN.Connection = con;
            try
            {
                UPDATE_RTHDR.Transaction = tran;
                UPDATE_RTHDR.CommandText = "UPDATE_RTHDR";
                UPDATE_RTHDR.CommandType = CommandType.StoredProcedure;
                UPDATE_RTHDR.Parameters.Add(new SqlParameter("@InvNo", invno));
                UPDATE_RTHDR.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
                UPDATE_RTHDR.Parameters.Add(new SqlParameter("@SiteCode", "khoi"));
                UPDATE_RTHDR.Parameters.Add(new SqlParameter("@CustomerKey", txtMaSoKhachHang.Text.Trim()));
                UPDATE_RTHDR.Parameters.Add(new SqlParameter("@TotalAmount", float.Parse(lblTongTien.Text)));
                UPDATE_RTHDR.Parameters.Add(new SqlParameter("@InvDate", txtNgay.Text.Trim()));
                if (CheckChot.Checked)
                {
                    UPDATE_RTHDR.Parameters.Add(new SqlParameter("@Posted", "True"));
                }
                else
                {
                    UPDATE_RTHDR.Parameters.Add(new SqlParameter("@Posted", "False"));
                }
                UPDATE_RTHDR.ExecuteNonQuery();
                DELETE_RTLIN.Transaction = tran;
                DELETE_RTLIN.CommandText = "DELETE_RTLIN";
                DELETE_RTLIN.CommandType = CommandType.StoredProcedure;
                DELETE_RTLIN.Parameters.Add(new SqlParameter("@InvNo", invno));
                DELETE_RTLIN.ExecuteNonQuery();
                int i = 0;
                foreach (GridViewRow grv in GridView1.Rows)
                {
                    Insert_RTLIN.Parameters.Clear();
                    Insert_RTLIN.Transaction = tran;
                    Insert_RTLIN.CommandText = "INSERT_RTLIN";
                    Insert_RTLIN.CommandType = CommandType.StoredProcedure;
                    CheckBox chon;
                    Insert_RTLIN.Parameters.Add(new SqlParameter("@InvNo", invno));
                    chon = (CheckBox)GridView1.Rows[i].FindControl("CheckKM");
                    if (chon.Checked)
                    {
                        Insert_RTLIN.Parameters.Add(new SqlParameter("@LineType", "True"));
                    }
                    else
                    {
                        Insert_RTLIN.Parameters.Add(new SqlParameter("@LineType", "False"));
                    }
                    Insert_RTLIN.Parameters.Add(new SqlParameter("@itemkey", grv.Cells[1].Text));
                    Insert_RTLIN.Parameters.Add(new SqlParameter("@Quantity", float.Parse(grv.Cells[3].Text)));
                    Insert_RTLIN.Parameters.Add(new SqlParameter("@Price", float.Parse(grv.Cells[5].Text)));
                    Insert_RTLIN.ExecuteNonQuery();
                    i++;
                }
                UPDATE_FILTER_RTARCUST.Transaction = tran;
                UPDATE_FILTER_RTARCUST.CommandText = "UPDATE_FILTER_RTARCUST";
                UPDATE_FILTER_RTARCUST.CommandType = CommandType.StoredProcedure;
                UPDATE_FILTER_RTARCUST.Parameters.Add(new SqlParameter("@CustomerKey", txtMaSoKhachHang.Text.Trim()));
                UPDATE_FILTER_RTARCUST.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
                UPDATE_FILTER_RTARCUST.Parameters.Add(new SqlParameter("@LastBuy", txtNgay.Text));
                UPDATE_FILTER_RTARCUST.Parameters.Add(new SqlParameter("@UTDOffTake", float.Parse(lblTongTien.Text)));
                UPDATE_FILTER_RTARCUST.ExecuteNonQuery();
                tran.Commit();
                lblKetQua.Text = "Cập nhật phiếu thu hoàn thành";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                lblKetQua.Text = ex.Message;
            }
            finally
            {
                con.Close();
            }
        }
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        clear();
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        TabContainer1.ActiveTabIndex = 1;
        MultiView1.ActiveViewIndex = 2;
        lblKetQua.Visible = false;
    }
    protected void ImageTimTuVanVien_Click(object sender, ImageClickEventArgs e)
    {
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        TabContainer1.ActiveTabIndex = 1;
        MultiView1.ActiveViewIndex = 1;
        lblKetQua.Visible = false;
    }
    void viewKhachHang()
    {
        dal = new BCDataCenter.DataAccessLayer.DAL();
        DataSet dsKhachHang = dal.MyExecuteQuery(ref error, "SEARCH_RTARCUST", CommandType.StoredProcedure, new SqlParameter("@parameter", txtSearchKhachHang.Text));
        try
        {
            if (dsKhachHang.Tables[0].Rows.Count == 1)
            {
                txtMaSoKhachHang.Text = dsKhachHang.Tables[0].Rows[0][0].ToString();
                txtTenKhachHang.Text = dsKhachHang.Tables[0].Rows[0][1].ToString();
                txtDiaChi.Text = dsKhachHang.Tables[0].Rows[0][2].ToString();
                dsKhachHang.Dispose();
                TabContainer1.ActiveTabIndex = 0;
            }
            else
            {
                GridView2.DataSource = dsKhachHang.Tables[0];
                GridView2.DataBind();
            }
        }
        catch
        {
            uscMsgBox1.AddMessage("Không tồn tại khách hàng", uscMsgBox.enmMessageType.Error); 
        }
    }
    protected void txtViewKhachHang_Click(object sender, ImageClickEventArgs e)
    {
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        viewKhachHang();
        lblKetQua.Visible = false;
    }
    void viewTuVanVien()
    {
        dal = new BCDataCenter.DataAccessLayer.DAL();
        DataSet dsTuVanVien = dal.MyExecuteQuery(ref error, "SEARCH_SALESPKEY", CommandType.StoredProcedure, new SqlParameter("@parameter", txtSearchTenTuVanVien.Text));
        if (dsTuVanVien.Tables[0].Rows.Count == 1)
        {
            txtMaTuVanVien.Text = dsTuVanVien.Tables[0].Rows[0][0].ToString();
            txtTuVanVien.Text = dsTuVanVien.Tables[0].Rows[0][1].ToString();
            dsTuVanVien.Dispose();
            TabContainer1.ActiveTabIndex = 0;
        }
        else
        {
            GridView3.DataSource = dsTuVanVien.Tables[0];
            GridView3.DataBind();
        }
    }
    protected void TuVanVienView_Click(object sender, ImageClickEventArgs e)
    {
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        viewTuVanVien();
        lblKetQua.Visible = false;
    }
    protected void txtSearchKhachHang_TextChanged(object sender, EventArgs e)
    {
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        viewKhachHang();
        lblKetQua.Visible = false;
    }
    protected void txtSearchTenTuVanVien_TextChanged(object sender, EventArgs e)
    {
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        viewTuVanVien();
        lblKetQua.Visible = false;
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        viewKhachHang();
    }
    protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView3.PageIndex = e.NewPageIndex;
        viewTuVanVien();
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            lblTitle.Visible = false;
            lblSoChungTu.Visible = false;
             GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
             txtMaSoKhachHang.Text = grv.Cells[0].Text;
             txtTenKhachHang.Text = grv.Cells[1].Text;
             txtDiaChi.Text = grv.Cells[2].Text;
             TabContainer1.ActiveTabIndex = 0;
             lblKetQua.Visible = false;
             this.GridView2.DataSource = null;
             this.GridView2.DataBind();
        }
    }
    protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            txtMaTuVanVien.Text = grv.Cells[0].Text;
            txtTuVanVien.Text = grv.Cells[1].Text;
            TabContainer1.ActiveTabIndex = 0;
            lblKetQua.Visible = false;
            this.GridView3.DataSource = null;
            this.GridView3.DataBind();
        }
    }
    protected void imgXoa_Click(object sender, ImageClickEventArgs e)
    {
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        lblKetQua.Visible = true;
        con = new SqlConnection(str);
        con.Open();
        tran = con.BeginTransaction(IsolationLevel.Serializable);
        DELETE_RTLIN = new SqlCommand("DELETE_RTLIN", con);
        SqlCommand DELETE_RTHDR = new SqlCommand("DELETE_RTHDR", con);
        try
        {
            DELETE_RTHDR.Transaction = tran;
            DELETE_RTHDR.CommandType = CommandType.StoredProcedure;
            DELETE_RTHDR.Parameters.Add(new SqlParameter("@InvNo", ""));
            DELETE_RTHDR.ExecuteNonQuery();
            DELETE_RTLIN.Transaction = tran;
            DELETE_RTLIN.CommandType = CommandType.StoredProcedure;
            DELETE_RTLIN.Parameters.Add(new SqlParameter("@InvNo", ""));
            DELETE_RTLIN.ExecuteNonQuery();
            tran.Commit();
            lblKetQua.Text = "Đã xóa dữ liệu";
            clear();
        }
        catch(Exception ex)
        {
            lblKetQua.Text=ex.Message;
            tran.Rollback();
        }
        finally
        {
            con.Close();
        }

    }
    protected void PhieuMuaHangView_Click(object sender, ImageClickEventArgs e)
    {
        
        displayUpdate();
        lblKetQua.Visible = false;
    }
    void displayUpdate()
    {
        common = new MethodClassCommon();
        dal = new BCDataCenter.DataAccessLayer.DAL();
        DataSet dsPhieuMuaHang = dal.MyExecuteQuery(ref error, "SEARCH_INVNO", CommandType.StoredProcedure, new SqlParameter("@parameter", txtSearchPhieuMuaHang.Text));
        if (dsPhieuMuaHang.Tables[0].Rows.Count == 1)
        {
            lblTitle.Visible = true;
            lblSoChungTu.Visible = true;
            ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Clear();
            DataSet dsSanPham = dal.MyExecuteQuery(ref error, "GET_RTLIN", CommandType.StoredProcedure, new SqlParameter("@InvNo", dsPhieuMuaHang.Tables[0].Rows[0][0].ToString()));
            
            for (int i = 0; i < dsSanPham.Tables[0].Rows.Count; i++)
            {
                ClassPhieuMuaHang rs = new ClassPhieuMuaHang();
                rs.MaHang1 = dsSanPham.Tables[0].Rows[i][1].ToString();
                rs.TenHang1 = common.GetDataDepend("GET_ItemName","itemkey",rs.MaHang1.ToString());
                rs.SoLuong1 = float.Parse(dsSanPham.Tables[0].Rows[i][3].ToString());
                rs.Gia1 = float.Parse(dsSanPham.Tables[0].Rows[i][4].ToString());
                if (dsSanPham.Tables[0].Rows[i][2].ToString() == "True")
                {
                    rs.LineType1 = true;
                }
                else
                {
                    rs.LineType1 = false;
                }
                ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Add(rs);
            }
            GridView1.DataSource = Session["phieumuahang"];
            GridView1.DataBind();
            dsSanPham.Dispose();
            DataSet dsPhieu = dal.MyExecuteQuery(ref error, "GET_CUST_INVNO", CommandType.StoredProcedure, new SqlParameter("@InvNo", dsPhieuMuaHang.Tables[0].Rows[0][0].ToString()));
            dsPhieuMuaHang.Dispose();
            lblSoChungTu.Text = dsPhieu.Tables[0].Rows[0][0].ToString();
            txtMaTuVanVien.Text = dsPhieu.Tables[0].Rows[0][1].ToString();
            txtMaSoKhachHang.Text = dsPhieu.Tables[0].Rows[0][3].ToString();
            lblTongTien.Text = dsPhieu.Tables[0].Rows[0][4].ToString();
            txtNgay.Text = dsPhieu.Tables[0].Rows[0][5].ToString();
            if (dsPhieu.Tables[0].Rows[0][6].ToString() == "True")
            {
                CheckChot.Checked = true;
            }
            else
            {
                CheckChot.Checked = false;
            }
            txtTenKhachHang.Text = dsPhieu.Tables[0].Rows[0][7].ToString();
            txtDiaChi.Text = dsPhieu.Tables[0].Rows[0][8].ToString();
            dsPhieu.Dispose();
            txtTuVanVien.Text = common.GetDataDepend("GET_SALESPNAME", "salesp_key", txtMaTuVanVien.Text.Trim());
            TabContainer1.ActiveTabIndex = 0;
            flag = false;
        }
        else
        {
            GridView4.DataSource = dsPhieuMuaHang.Tables[0];
            GridView4.DataBind();
        }
    }
    protected void txtSearchPhieuMuaHang_TextChanged(object sender, EventArgs e)
    {
        displayUpdate();
        lblKetQua.Visible = false;
    }
    protected void GridView4_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView4.PageIndex = e.NewPageIndex;
        displayUpdate();
    }
    protected void GridView4_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            common = new MethodClassCommon();
            lblTitle.Visible = true;
            lblSoChungTu.Visible = true;
            lblKetQua.Visible = false;
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            dal = new BCDataCenter.DataAccessLayer.DAL();
            ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Clear();
            DataSet dsSanPham = dal.MyExecuteQuery(ref error, "GET_RTLIN", CommandType.StoredProcedure, new SqlParameter("@InvNo",grv.Cells[0].Text));

            for (int i = 0; i < dsSanPham.Tables[0].Rows.Count; i++)
            {
                ClassPhieuMuaHang rs = new ClassPhieuMuaHang();
                rs.MaHang1 = dsSanPham.Tables[0].Rows[i][1].ToString();
                rs.TenHang1 = common.GetDataDepend("GET_ItemName", "itemkey", rs.MaHang1.ToString());
                rs.SoLuong1 = float.Parse(dsSanPham.Tables[0].Rows[i][3].ToString());
                rs.Gia1 = float.Parse(dsSanPham.Tables[0].Rows[i][4].ToString());

                if (dsSanPham.Tables[0].Rows[i][2].ToString() == "True")
                {
                    rs.LineType1 = true;
                }
                else
                {
                    rs.LineType1 =false;
                }
                ((List<ClassPhieuMuaHang>)Session["phieumuahang"]).Add(rs);
            }
            GridView1.DataSource = Session["phieumuahang"];
            GridView1.DataBind();
            dsSanPham.Dispose();
            DataSet dsPhieu = dal.MyExecuteQuery(ref error, "GET_CUST_INVNO", CommandType.StoredProcedure, new SqlParameter("@InvNo", grv.Cells[0].Text));
            lblSoChungTu.Text = dsPhieu.Tables[0].Rows[0][0].ToString();
            txtMaTuVanVien.Text = dsPhieu.Tables[0].Rows[0][1].ToString();
            txtMaSoKhachHang.Text = dsPhieu.Tables[0].Rows[0][3].ToString();
            lblTongTien.Text = dsPhieu.Tables[0].Rows[0][4].ToString();
            txtNgay.Text = dsPhieu.Tables[0].Rows[0][5].ToString();
            if (dsPhieu.Tables[0].Rows[0][6].ToString() == "True")
            {
                CheckChot.Checked = true;
            }
            else
            {
                CheckChot.Checked = false;
            }
            txtTenKhachHang.Text = dsPhieu.Tables[0].Rows[0][7].ToString();
            txtDiaChi.Text = dsPhieu.Tables[0].Rows[0][8].ToString();
            dsPhieu.Dispose();
            txtTuVanVien.Text = common.GetDataDepend("GET_SALESPNAME", "salesp_key", txtMaTuVanVien.Text.Trim());
            TabContainer1.ActiveTabIndex = 0;
            flag = false;
            this.GridView4.DataSource = null;
            this.GridView4.DataBind();
        }
    }
    protected void txtMaSoKhachHang_TextChanged(object sender, EventArgs e)
    {
        common = new MethodClassCommon();
        lblTitle.Visible = false;
        lblSoChungTu.Visible = false;
        lblKetQua.Visible = false;
        try
        {
            txtTenKhachHang.Text = common.GetDataDepend("GET_CustomerName", "CustomerKey", txtMaSoKhachHang.Text.Trim());
        }
        catch
        {
            uscMsgBox1.AddMessage("Mã khách hàng không đúng,nhập lại",uscMsgBox.enmMessageType.Error);
            txtTenKhachHang.Text = "";
        }
       
       
    }
    protected void txtMaTuVanVien_TextChanged(object sender, EventArgs e)
    {
        try
        {

            txtTuVanVien.Text = txtMaTuVanVien.Text.Substring(txtMaTuVanVien.Text.IndexOf(Convert.ToChar("|")) + 2);
            txtMaTuVanVien.Text = txtMaTuVanVien.Text.Substring(0, txtMaTuVanVien.Text.IndexOf(Convert.ToChar("|")));
        }
        catch
        {
            return;
        }
       
        
    }
}
